/**
 * 该文件用于对登录相关接口，做 API 封装
 *
 * Promise<A>     => 表示是一个 Promise, A 表示该 Promise .then 回调函数的参数类型
 */

import { getRefreshToken } from "@/utils";
import request from "@/utils/request";

/**
 * 获取验证码
 */
export function getCaptcha() {
  return request<Login.Captcha>({
    method: "GET",
    url: "/admin/base/open/captcha",
  });
}

export function loginApi(data: Login.Params) {
  return request<Login.Resp>({
    method: "POST",
    url: "/admin/base/open/login",
    data,
  });
}

/**
 * 刷新TOKEN
 */
let promise: any = null;

export function refreshToken() {
  console.log("刷新token");
  if (promise) {
    return promise;
  }

  promise = new Promise((resolve, reject) => {
    request<Login.Resp>({
      url: "/admin/base/open/refreshToken",
      method: "GET",
      params: {
        refreshToken: getRefreshToken(),
      },
    })
      .then((resp) => {
        resolve(resp);

        // 将 promise 设置为 null
        promise = null;
      })
      .catch((error) => {
        reject(error);
      });
  });

  return promise;
}

// interface HelloResponse<T = any> {
//   headers: any
//   data: T
// }

// function Test<T = any, R = HelloResponse<T> >(): Promise<R> {

// }

// Test<string[]>().then((resp) => {
//   resp.headers
//   resp.data
// })
